import Vue from 'vue'
import VueRouter from 'vue-router'
// import Home from '../views/Home.vue'
const Login = () => import('@/views/login')
const Home = () => import('@/views/home/index.vue')
const Article = () => import('@/views/article/index.vue')
const Publish = () => import('@/views/publish/index.vue')
const Layout = () => import('@/views/layout/index.vue')
import { getitem } from '../utils/auth'
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    redirect: '/home',
    children: [
      {
        path: '/home',
        component: Home
      },
      {
        path: '/article',
        component: Article
      },
      {
        path: '/publish',
        component: Publish
      }
    ]
  }
  // {
  // path: '/about',
  // name: 'About',
  // route level code-splitting
  // this generates a separate chunk (about.[hash].js) for this route
  // which is lazy-loaded when the route is visited.
  // component: () =>
  // import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, from, next) => {
  console.log(to, from)
  Nprogress.start()
  if (getitem()) {
    if (to.path === '/') {
      next('/home')
    } else {
      next()
    }
  } else {
    if (to.path === '/') {
      next()
    } else {
      next('/')
    }
  }
  Nprogress.done()
})
router.afterEach((to, from) => {
  Nprogress.done()
  console.log('afterEach', to, from)
})

export default router
